<template>
	<view class="container" v-if="isshow">
		<view class="tui-news-title">
			{{info.post_title}}
		</view>
		<view class="tui-sub-info">
			<view class="tui-sub-left">
				<text class="tui-author">{{info.user.user_nickname}}</text>
				<text>{{info.published_time}}</text>
			</view>
			<view class="tui-sub-right">阅读 {{info.post_hits}}</view>
		</view>
		<view class="tui-news-content">
			<view v-if="info.more.video" class="tui-article">
				<video :src="info.more.video" controls></video>
			</view>
			<view class="tui-article">
				<rich-text :nodes="info.post_content"></rich-text>
			</view>
		</view>

		<view class="tui-safearea-bottom"></view>
	</view>
</template>

<script>
	var FAIL_CONTENT = '<p>获取信息失败</p>',
		api = require('@/common/api.js')
	export default {
		onShareAppMessage(res) {
			return {
			  title: this.info.post_title,
			  path: '/pages/detail/detail?id='+this.info.id
			}
		},
		data() {
			return {
				info:{},
				isshow:false,
			}
		},
		onLoad(event) {
			api.get({
				url: 'order/articles',
				data:{id:event.id},
				success: (result) => {
					
					result.data.post_content = this.formatRichText(result.data.post_content)
					if (result.code == 1) {
						this.info = result.data
					} else {
						this.content = FAIL_CONTENT;
					}
					this.isshow = true
				}
			});
		},
		methods: {
			/**
			 * 处理富文本里的图片宽度自适应
			 * 1.去掉img标签里的style、width、height属性
			 * 2.img标签添加style属性：max-width:100%;height:auto
			 * 3.修改所有style里的width属性为max-width:100%
			 * 4.去掉<br/>标签
			 * @param html
			 * @returns {void|string|*}
			 */

			formatRichText(html) {
				let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
					match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
					match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
					match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
					return match;
				});
				newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
					match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
					return match;
				});
				newContent = newContent.replace(/<br[^>]*\/>/gi, '');
				newContent = newContent.replace(/\<img/gi,
					'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
				return newContent;
			}
		},
		
	}
</script>

<style>
	page {
		background: #fff;
		color: #333;
	}
	video {
		width: 100%;
	}
	.container {
		padding: 40rpx 30rpx 110rpx 30rpx;
		box-sizing: border-box;
	}

	.tui-news-title {
		font-size: 48rpx;
		font-weight: 500;
		text-align: justify;
	}

	
	.tui-news-content {
		padding-top: 40rpx;
	}

	.tui-article {
		/* text-indent: 2em; */
		font-size: 34rpx;
		padding-bottom: 40rpx;
		line-height: 60rpx;
		text-align: justify;
		word-break: break-all;
		word-wrap: break-word;
	}

	.tui-article-pic {
		width: 100%;
		display: block;
		margin-bottom: 40rpx;
	}
	
	.tui-sub-info {
		padding-top: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		color: #999;
	}

	.tui-author {
		color: #5677fc;
		padding-right: 20rpx;
	}
	
</style>
